package com.ider.launcherpackage.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;


public class InstallUtil2 {
//////////////////////此类为InstallUtil类的修改版  周若熹2018.5.2日
    public static final String TAG = "zhao";
    public static final int INSTALL_SUCCESS = 0;
    public static final int INSTALL_FILE_NOTFOUND = 1;
    public static final int INSTALL_ERROR = 2;

    private InstallUtil2() {
        throw new AssertionError();
    }

    /**
     * @param filePath
     * @return 0 means normal, 1 means file not exist, 2 means other exception error
     */

    public static boolean installSlient2(String filePath) {

        Log.e(TAG, "path = " + filePath);
        File file;
        if (filePath == null || filePath.length() == 0 || !(file = new File(filePath)).exists() || file.length() <= 0 || !file.isFile()) {
            Log.e(TAG, "1");
            return false;
        }

        String[] args = {"pm", "install", "-r", filePath};
        ProcessBuilder processBuilder = new ProcessBuilder(args);

        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder errorMsg = new StringBuilder();
        int result;
        try {
            process = processBuilder.start();
            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;

            while ((s = successResult.readLine()) != null) {
                successMsg.append(s);
            }

            while ((s = errorResult.readLine()) != null) {
                errorMsg.append(s);
            }
        } catch (Exception e) {
            Log.e(TAG, ""+e.toString());
            e.printStackTrace();
        }finally {
            try {
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                Log.e(TAG, "InstallUtil: "+e.toString());
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
        if (successMsg.toString().contains("Success")) {
            Log.e(TAG, "InstallUtil: install Success!!!");
            return true;
        } else {
            Log.e(TAG, "InstallUtil:install Failed!!!");
            return false;
        }
    }

    public static void playApk(Context context,File file){
        Intent apkIntent = new Intent();
        apkIntent.setAction(android.content.Intent.ACTION_VIEW);
        apkIntent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        try {
            context.startActivity(apkIntent);
        } catch (ActivityNotFoundException e) {
           e.printStackTrace();
           Log.d("zhao","playApk = "+e.toString());
        }

    }
}